#pragma once

#include "TableCommon.h"
#include "TableColumn.h"
#include "SortBuffer.h"

//#include "TableSorter.h"
#include "JumpList.h"
#include <vector>

namespace qb{
	namespace ht{
		class Table;
		class TableSorter;
        typedef std::vector<kColumn> ColumnVec;
		class IntVec : public std::vector < int > {
		public:
			IntVec(){}
			IntVec(const char* jsonList);
		};
		class GridColumnSet{
		public:
			ColumnVec		m_select_columns;	// 原始表格中只有这些才是可见的，用户在界面上可以配置
			ColumnVec		m_visible_columns;	// 可见列，以界面显示顺序，已经排序
			ColumnVec		m_sort_columns;		// 多重排序
			BoolVec			m_sort_desc_flags;	// 默认为false,表示递增，否则表示递减
		public:
			void SetSelection(const IntVec& columns);
			void SetVisible(const IntVec& columns);
			void SetMultiSort(const IntVec& columns);
			void SetSortDecFlags(const IntVec& columns);
		};
		class TableFilter{
		public:
			virtual bool IsRecordNeed(RecordId rid, const void* record, int size);
		};
		class TableSearcher{
		public:
			virtual bool IsRecordNeed(RecordId rid, const void* record, int size);
		};
		class TableSummary{
		};

		enum kTableUserType{
			kTableUserTypeGrid,
			kTableUserTypeCount
		};
        class TableUser{

			struct RowView{
				unsigned int row, rid;
				unsigned short view, first;
			};
            class RowViewBuffer : public std::vector<RowView> {
			public:
				static RowViewBuffer& Get(){
					static RowViewBuffer rvb;
					return rvb;
				}
			};
		public:
			struct RecordKey{
				int rid;
			public:
				RecordKey(){}
				bool operator==(const RecordKey& key)const{ return rid == key.rid; }
				//bool operator<(const RecordKey& bki)const{ return true; }
				unsigned int GetHash()const;
				void SetSortId(int sortid){}
				void SetChanged(int changeid){}
				int  GetChanged(){ return 0; }
			};
			
			class RecordComparator{
				TableUser*		m_user;
			public:
				RecordComparator(TableUser* user) :m_user(user){}
				int GetExpandLength(int count, int length){ return length * 2; }
				int operator()(const RecordKey& k1, const RecordKey& k2)const{
					return m_user->OnCompare(k1, k2);
				}
			};
			typedef JumpList<RecordKey, RecordComparator> SortedRecordList;
			typedef SortedRecordList::KeyIterator KeyIterator;
            typedef std::vector<TableFilter*> TableFilterVec;
		protected:
            void*					m_user_window;		// 用户窗口
			int						m_stateid;			// 状态id
			kTableUserType			m_user_type;		// 用户类型
			TableSearcher*			m_searcher;			// 搜索过滤对象
			TableFilterVec			m_filters;			// 过滤器
			TableSummary*			m_summary;			// 总结器
			GridColumnSet			m_grid_columns;		// 列信息
			Table*					m_table;			// 用于排序时加载记录
			SortedRecordList*		m_record_list;		// 有序队列
			HTUserId				m_userid;
			friend class TableSorter;
		public:
            TableUser(const GridColumnSet& columns, void* grid);
			virtual ~TableUser();
			HTUserId GetId()const{ return m_userid; }
			void SetTable(Table* table){ m_table = table; }
            void* GetWindow(){ return m_user_window; }

			void ClearSortColumns();
			void AddSortColumn(int column, bool ascend);
			const ColumnVec& GetSortColumns(){ return m_grid_columns.m_sort_columns; }

			bool IsRecordNeeded(RecordId rid, const void* record, int size);
			bool InsertRow(Table* table, RecordId rid, const void* record, int size, const BoolVec&, bool newRecord);

			bool GetColumnDecFlag(int index)const{
				if (index >= 0 && index < (int)m_grid_columns.m_sort_desc_flags.size())
					return m_grid_columns.m_sort_desc_flags.at(index);
				return false;
			}

			// 作为SortedRecordList的模板参数所要求的接口
			int  GetHashLength(int count, int length){ return (int)(length*1.5); }
			bool OnBeforeSort(){ return true; }
			void OnAfterSort(){}
			int  OnCompare(const RecordKey& k1, const RecordKey& k2);

			// 获取迭代器,用于遍历访问已经排序的列表;获取指定范围的记录
			KeyIterator GetBegin();
			KeyIterator GetEnd();
			int GetRecordCount()const;
            void GetKeys(int index, int count, std::vector<RecordKey>& keys);

			// 将已经排序的信息更新到SortedRecordList中;
            void UpdateIndex(const std::vector<INDEX>& indexes);
			void UpdateIndex(SortBuffer* sortBuffer);
		protected:
			bool HasSortColumns()const{ return m_grid_columns.m_sort_columns.size() > 0; }

			int CompareRecord(Table* table, const void* left, const void* right);
			bool IsSortColumnChanged(const BoolVec& fieldChangeFlags);
		};
	}//namespace ht
}//namespace qb
